﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SWCInvoice.Model;
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using AvalonDock;

namespace SWCInvoice.ViewModel {
    public class VirtualDocumentStructureViewModel : ViewModelBase {
        
        #region Fields

        /// <summary>
        /// The list of virtual document structure
        /// </summary>
        ObservableCollection<VirtualDocumentStructure> _virtualDocumentStructures;
        
        /// <summary>
        /// Gets or sets current VirtualDocumentStructure
        /// </summary>
        VirtualDocumentStructure _currentVirtualDocumentStructure;

        private RelayCommand _PdfViewCommand;

        private MainWindow _mainWindow;

        /// <summary>
        /// Gets the current state if the user can set the identifier
        /// </summary>
        private bool _canSetIdentifier;

        /// <summary>
        /// Gets the current state if the user can delete the identifier
        /// </summary>
        private bool _canDeleteIdentifier;

        /// <summary>
        /// Gets the current state if the user can sort documents
        /// </summary>
        private bool _canSort;

        /// <summary>
        /// Gets the current state if the user can split pages
        /// </summary>
        private bool _canSplit;

        /// <summary>
        /// Gets the current state if the user can merge pages
        /// </summary>
        private bool _canMerge;

        /// <summary>
        /// Gets the current state if the user can paste pages
        /// </summary>
        private bool _canPaste;

        /// <summary>
        /// Gets the current state if the user can save the vds
        /// </summary>
        private bool _canSave;

        /// <summary>
        /// Gets the current state if the user can close the vds
        /// </summary>
        private bool _canClose;

        #endregion

        #region Properties

        /// <summary>
        /// The list of virtual document structure
        /// </summary>
        public ObservableCollection<VirtualDocumentStructure> VirtualDocumentStructures {
            get {
                return _virtualDocumentStructures == null ? _virtualDocumentStructures = new ObservableCollection<VirtualDocumentStructure>() : _virtualDocumentStructures;
            }
            set {
                if (_virtualDocumentStructures == value)
                    return;
                _virtualDocumentStructures = value;
                RaisePropertyChanged("VirtualDocumentStructures");
            }
        }

        /// <summary>
        /// Gets current VirtualDocumentStructure
        /// </summary>
        public VirtualDocumentStructure CurrentVirtualDocumentStructure {
            get {
                return _currentVirtualDocumentStructure;
            }
            set {
                if (_currentVirtualDocumentStructure == value)
                    return;
                _currentVirtualDocumentStructure = value;
                RaisePropertyChanged("CurrentVirtualDocumentStructure");

                if (VirtualDocumentStructures.Count == 0) {
                    CanDeleteIdentifier = CanSetIdentifier = CanSort = CanSplit = CanMerge = CanPaste = CanSave = CanClose = false;
                } else {
                    CanDeleteIdentifier = CanSetIdentifier = CanSort = CanSplit = CanMerge = CanPaste = CanSave = CanClose = true;
                }
            }
        }

        /// <summary>
        /// Gets the current state if the user can set the identifier
        /// </summary>
        public bool CanSetIdentifier {
            get {
                return _canSetIdentifier;
            }
            set {
                if (_canSetIdentifier == value)
                    return;
                _canSetIdentifier = value;
                RaisePropertyChanged("CanSetIdentifier");
            }
        }

        /// <summary>
        /// Gets the current state if the user can delete the identifier
        /// </summary>
        public bool CanDeleteIdentifier {
            get {
                return _canDeleteIdentifier;
            }
            set {
                if (_canDeleteIdentifier == value)
                    return;
                _canDeleteIdentifier = value;
                RaisePropertyChanged("CanDeleteIdentifier");
            }
        }

        /// <summary>
        /// Gets the current state if the user can sort documents
        /// </summary>
        public bool CanSort {
            get {
                return _canSort;
            }
            set {
                if (_canSort == value)
                    return;
                _canSort = value;
                RaisePropertyChanged("CanSort");
            }
        }

        /// <summary>
        /// Gets the current state if the user can split pages
        /// </summary>
        public bool CanSplit {
            get {
                return _canSplit;
            }
            set {
                if (_canSplit == value)
                    return;
                _canSplit = value;
                RaisePropertyChanged("CanSplit");
            }
        }

        /// <summary>
        /// Gets the current state if the user can merge pages
        /// </summary>
        public bool CanMerge {
            get {
                return _canMerge;
            }
            set {
                if (_canMerge == value)
                    return;
                _canMerge = value;
                RaisePropertyChanged("CanMerge");
            }
        }

        /// <summary>
        /// Gets the current state if the user can paste pages
        /// </summary>
        public bool CanPaste {
            get {
                return _canPaste;
            }
            set {
                if (_canPaste == value)
                    return;
                _canPaste = value;
                RaisePropertyChanged("CanPaste");
            }
        }

        /// <summary>
        /// Gets the current state if the user can save the vds
        /// </summary>
        public bool CanSave {
            get {
                return _canSave;
            }
            set {
                if (_canSave == value)
                    return;
                _canSave = value;
                RaisePropertyChanged("CanSave");
            }
        }

        /// <summary>
        /// Gets the current state if the user can close the vds
        /// </summary>
        public bool CanClose {
            get {
                return _canClose;
            }
            set {
                if (_canClose == value)
                    return;
                _canClose = value;
                RaisePropertyChanged("CanClose");
            }
        }

        #region Commands

        public RelayCommand PdfViewCommand
        {
            get
            {
                if (_PdfViewCommand == null)
                    _PdfViewCommand = new RelayCommand(param => OpenPdfView_Click());
                return _PdfViewCommand;
            }
        }

        #endregion


        #region Windows

        /// <summary>
        /// The Main Window
        /// </summary>
        public MainWindow MainWindow
        {
            get
            {
                return _mainWindow;
            }

            set
            {
                _mainWindow = value;
            }
        }

        #endregion

        #endregion


        #region Command Methods

        private void OpenPdfView_Click()
        {
            Windows.PdfView pdfViewWindow = new Windows.PdfView();
            //_helpWindow.Show();

            //DockableContent newContent = new DockableContent();
            DocumentContent newContent = new DocumentContent();

            DockableContent existingContent = GetContentByName("oHelpWindow");
            if (existingContent == null)
            {
                newContent.Title = Properties.ResourceHelp.WindowTitle;
                newContent.Name = "oPdfViewWindow";
                newContent.Content = pdfViewWindow;
                //newContent.Show(MainWindow.dockingManager);
                newContent.Show(MainWindow.dockingManager);

                //dockingManager.Show(newContent);
            }
            else if (existingContent.State == DockableContentState.Hidden)
                existingContent.Show(MainWindow.dockingManager);
            //dockingManager.Show(existingContent);
        }
        
        #endregion


        #region Methods

        /// <summary>
        /// Get existing dockable content
        /// </summary>
        /// <param name="name"></param>
        /// <returns>The dockable content</returns>
        public DockableContent GetContentByName(string name)
        {
            return MainWindow.dockingManager.DockableContents.Where(dc => dc.Name == name).FirstOrDefault();
        }

        #endregion
    }
}
